/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.schedule.core.domain;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.cron.pattern.CronPatternBuilder;
import cn.hutool.cron.pattern.Part;
import info.peigen.hotpot.common.core.domain.DomainObject;
import info.peigen.hotpot.common.core.enums.YesOrNo;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import info.peigen.hotpot.component.schedule.facade.command.order.ScheduleNodeCreateOrder;
import jakarta.validation.constraints.NotEmpty;
import lombok.*;
import lombok.experimental.Accessors;
import lombok.experimental.FieldDefaults;
import lombok.experimental.SuperBuilder;

/**
 * <b>(ScheduleNode)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/12/28
 */
@EqualsAndHashCode(callSuper = true)
@SuperBuilder
@Data
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Accessors(chain = true)
public class ScheduleNode extends DomainObject {
    String nodeName;
    @NotEmpty
    String ip;
    @NotEmpty
    String port;
    /** 随机定时器,默认范围5-15秒，单位秒 */
    String cron;
    /** 是否可用状态 */
    @Builder.Default
    YesOrNo yesOrNo = YesOrNo.Y;
    String memo;

    public static String genCron() {
        return CronPatternBuilder.of()
                .set(Part.SECOND, "0/" + RandomUtil.randomInt(5, 15))
                .build();
    }

    public static ScheduleNode create(ProcessorServiceContext context) {
        ScheduleNodeCreateOrder order = (ScheduleNodeCreateOrder) context.getOrder();
        return BeanUtil.copyProperties(order, ScheduleNode.class);
    }
}
